package com.zzzy.framework.common.util;

import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;
import java.util.Objects;

public class ReflectionUtil extends ReflectionUtils {

    public static Object getFieldValue(Object obj, String fieldName) {
        if (Objects.isNull(obj) || Objects.isNull(fieldName)){
            return null;
        }
        Field field = ReflectionUtils.findField(obj.getClass(), fieldName);
        if (Objects.isNull(field)){
            return null;
        }
        ReflectionUtils.makeAccessible(field);
        return getField(field, obj);
    }

    public static void setFieldValue(Object obj, String fieldName, Object fieldValue) {
        if (Objects.isNull(obj) || Objects.isNull(fieldName)){
            return;
        }
        Field field = ReflectionUtils.findField(obj.getClass(), fieldName);
        if (Objects.isNull(field)){
            return;
        }
        ReflectionUtils.makeAccessible(field);
        setField(field, obj, fieldValue);
    }
}
